package com.heather.exam.controller.user;

import com.heather.exam.pojo.Result;
import com.heather.exam.pojo.table.Code_suit;
import com.heather.exam.pojo.table.User;
import com.heather.exam.service.CodeService;
import com.heather.exam.service.RegisterService;
//import com.heather.exam.until.StingUtilHkj;
import com.heather.exam.service.UserService;
import com.heather.exam.until.DateTimeUtils;
import com.heather.exam.until.JwtUtils;
import com.heather.exam.until.Register;
import com.heather.exam.until.StingUtilHkj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.time.LocalDateTime;
import java.util.*;

@RestController
public class RegisterController {
    @Autowired
    RegisterService registerService;
    @Autowired
    CodeService codeService;
    @Autowired
    UserService userService;
    StingUtilHkj stingUtilHkj = new StingUtilHkj();

    private Integer codesleep = 0;
    String authCode = StingUtilHkj.YZM();
    //获取验证码
    @GetMapping("/api/user/authcode")
    public Result Register(String email,HttpServletRequest request){
        if (email==null){
            return Result.error("邮箱不能为空");
        }
        String s = "";
        Register r= new Register();
        String clientIPAddress = stingUtilHkj.getClientIPAddress(request);
        LocalDateTime currentDateTime = DateTimeUtils.getCurrentDateTime();
        String time = DateTimeUtils.dateTimeToString(currentDateTime);

        Code_suit codeSuit = new Code_suit();
        codeSuit.setCode(StingUtilHkj.YZM());
        codeSuit.setIp(clientIPAddress);
        codeSuit.setTime(time);
        codeSuit.setEmail(email);
        List<Code_suit> check = codeService.check(email);
        if (check.size()>0){
            return Result.error("验证码发送太频繁");
        }
        try {
            codeService.save(codeSuit);
            s = r.sendEmail(email, authCode);
            return Result.success(s);
        } catch (MessagingException e) {
            e.printStackTrace();
            return Result.error("发送失败");
        }
    }
//    @GetMapping("/api/user/login")//若不封为对象最好还是用表单传输
//    public Result login(String account, String password){
//        System.out.println(account+"&"+password);
//        String pass = StingUtilHkj.MD5(password);
//        User e = userService.Login(account,pass);
//        if (e != null) {
//            Map<String, Object> claims = new HashMap<>();
//            claims.put("username", e.getUsername());
//            claims.put("email", e.getEmail());
//            claims.put("user_id", e.getId());
//            claims.put("user_status",e.getUser_status());
//            claims.put("user_role",e.getRoleId());
//
//            // 创建一个Calendar实例，设置为当前时间加上30分钟
//            Calendar calendar = Calendar.getInstance();
//            calendar.add(Calendar.MINUTE, 60);
//            Date expirationDate = calendar.getTime();
//
//            // 将过期时间添加到claims中
//            claims.put("exp", expirationDate);
//
//            // 生成包含过期时间的JWT
//            String jwt = JwtUtils.generateJwt(claims);
//            System.out.println(jwt);
//            return Result.success(jwt);
//        }
//        return Result.success(e);
//    }
    @PostMapping("/api/user/register")
    public Result Register(@RequestBody User user, @RequestParam String authCode){
        //数据获取
        String email = user.getEmail();//获取注册邮箱
        String encodedPassword = StingUtilHkj.MD5(user.getPassword());
        user.setPassword(encodedPassword);
        String code = codeService.getCode(email);
        //验证码判断
        if (authCode.equals(code)){
            //封装数据
            user.setEmail(user.getEmail());
            user.setUser_status((short)0);
            //用户查重
            Integer checking = registerService.checking(email);
            if (checking!=null){
                return Result.error("邮箱已注册");
            }
            //注册
            Integer i = registerService.visitorRegister(user);
            if (i>0){
                codeService.delete(email);
                return Result.success("添加成功");
            }
            return Result.error("未知错误");
        }
        return Result.error("验证码错误");
    }
}
